package org.csc.api.controller;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PublicController {

    private static final Logger log = LoggerFactory.getLogger(PublicController.class);

    @Autowired
    private DiscoveryClient client;

    @GetMapping("test")
    public String test() {
        List<String> services = client.getServices();
        services.forEach(s -> {
            client.getInstances(s).forEach(se -> log.info("服务信息, host:" + se.getHost() + "， service id:" + se.getServiceId()));
        });
//        if (true) {
//            throw new RuntimeException("测试");
//        }
        return "api服务";
    }

    @PostMapping("userInfo")
    public String userInfo() {
        JSONObject json = new JSONObject();
        json.put("id", "112321312");
        json.put("name", "测试");
        json.put("age", "22");
        json.put("address", "长沙");
        return json.toJSONString();
    }
}
